//# sourceURL=http://uMapApp/Earth/DataSourceView/C3DTilePrimitiveDataSourceView
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "./PrimitiveDataSourceView",
    "../ViewPort"
], function (
    mhjs,
    uMap,
    uEarth,
    PrimitiveDataSourceView,
    ViewPort
) {

    function C3DTilePrimitiveDataSourceView(option) {

        var _this = this;
        PrimitiveDataSourceView.call(_this, option);

        _this.tileSet = new uMap.ObliquePhotographyModelPrimitive({
            objectOption: option
        });

        _this.tileSet.on("loaded", function (tileSet) {

            _this.viewport = new ViewPort({center: tileSet.boundingSphere.center});

            if (option.style) {
                tileSet.style = new uEarth.Cesium3DTileStyle({
                    color: {
                        conditions: [
                            ["true", "color(\"white\"," + option.style.opacity + ")"]
                        ]
                    }
                });
            }

            _this.tileSet.on("click", function (event) {
                if (_this.layer.app.debug.debugTransform) {
                    _this.layer.app.debug.transformPanel.title = _this.title;
                    _this.layer.app.debug.transformPanel.transform = _this.tileSet.transform;
                    _this.layer.app.debug.transformPanel.open();
                }
                if (_this.layer.app.debug.debugFeature) {

                    if (event.pickObject) {

                        var feature = event.pickObject;

                        console.log(feature);
                        console.log("Class: " + feature.getExactClassName());

                        var propertyNames = feature.getPropertyNames();
                        var length = propertyNames.length;
                        for (var i = 0; i < length; ++i) {
                            var name = propertyNames[i];
                            var value = feature.getProperty(name);
                            console.log("  " + name + ": " + value);
                        }

                    }
                }
            });

            if (_this.loadFeatures) {

                _this.tileSet.object.allTilesLoaded.addEventListener(function () {
                    _this.getFeatures(_this.loadFeatures);
                });
            }

            _this.trigger("loaded");

        });


    }

    var _class = C3DTilePrimitiveDataSourceView;

    mhjs.Inherit(_class, PrimitiveDataSourceView);

    Object.defineProperties(_class.prototype, {
        "open"            : {
            value: function () {

                var _this = this;

                if (!_this.isOpened) {

                    _this.tileSet.open();
                    _this.isOpened = true;

                    this.trigger("open");
                    this.trigger("toggle");
                }
            }
        },
        "close"           : {
            value: function () {

                var _this = this;

                _this.tileSet.close();
                _this.isOpened = false;

                if (_this.isOpened) {
                    this.trigger("close");
                    this.trigger("toggle");
                }
            }
        },
        "setStyle"        : {
            value: function (nameStyles) {

                var _this = this;

                var conditions = [];
                $.each(nameStyles, function (i, nameStyle) {
                    conditions.push(["${" + nameStyle.tag + "}==='" + nameStyle.value + "'", nameStyle.style]);
                });
                conditions.push(["true", "color('white',1)"]);

                _this.tileSet.style = new uEarth.Cesium3DTileStyle({
                    "color": {
                        "conditions": conditions
                    }
                });
            }
        },
        "getFeatures"     : {
            value: function (key) {

                var _this = this;


                if (!_this.isFeaturesLoaded) {

                    _this.features = [];

                    $.each(_this.tileSet.object.root.children, function (i, tile) {

                        if (tile.content && tile.content.innerContents) {

                            $.each(tile.content.innerContents, function (i, innerContent) {

                                for (var j = 0; j < innerContent.featuresLength; j++) {
                                    var feature = innerContent.getFeature(j);

                                    if (feature && feature.getProperty && feature.getProperty(key)) {

                                        // if (!_this.features[feature.getProperty(key)]) {
                                        //     _this.features[feature.getProperty(key)] = [];
                                        // }

                                        _this.features.push(feature);


                                    }
                                }

                            });


                        } else if (tile.content) {

                            for (var k = 0; k < tile.content.featuresLength; k++) {
                                var feature = tile.content.getFeature(k);

                                if (feature && feature.getProperty && feature.getProperty(key)) {

                                    // if (!_this.features[feature.getProperty(key)]) {
                                    //     _this.features[feature.getProperty(key)] = [];
                                    // }

                                    _this.features.push(feature);
                                }


                            }
                        }
                    });

                    _this.trigger("featuresLoaded");
                    _this.isFeaturesLoaded = true;
                }


            }
        },
        "getFeaturesByKey": {
            value: function (key, search) {
                var _this = this;

                var result = [];

                if (_this.features) {
                    $.each(_this.features, function (i, feature) {
                        if (feature.getProperty && feature.getProperty(key) && feature.getProperty(key) === search) {
                            result.push(feature);
                        }
                    });
                }

                return result;
            }
        }
    });

    return _class;

});